package com.example.administrator.zhbj;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

import com.example.administrator.zhbj.utils.PrefUtils;

/**
 * 闪屏页
 * Created by Administrator on 2016/8/15 0015.
 */
public class SplashActivity extends Activity {

    RelativeLayout rlRoot;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        rlRoot = (RelativeLayout)findViewById(R.id.rl_root);


        startAnim();
    }

    /**
     * 开启动画
     */
    private  void startAnim(){

        //动画集合
        AnimationSet set = new AnimationSet(false);

        //旋转动画
        RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(1000);//动画时间
        rotate.setFillAfter(true);//保持动画状态

        //缩放动画
        ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scale.setDuration(1000);//动画时间
        scale.setFillAfter(true);//保持动画状态

        //渐变动画
        AlphaAnimation alpha = new AlphaAnimation(0, 1);
        alpha.setDuration(1000);//动画时间
        alpha.setFillAfter(true);//保持动画状态

        set.addAnimation(rotate);
        set.addAnimation(scale);
        set.addAnimation(alpha);

        //设置动画监听
        set.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }
            //动画执行结束
            @Override
            public void onAnimationEnd(Animation animation) {

                jumpNextPage();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        rlRoot.startAnimation(set);
    }

    /**
     * 跳转下一个页面
     */
    private void jumpNextPage(){
        //判断之前时候跳转过新手引导页
        //跳转到新手引导页
        boolean userGuide = PrefUtils.getBoolean(this, "is_user_guide_show", false);

        if(!userGuide){
            startActivity(new Intent(SplashActivity.this,GuideActivity.class));
        }else{
            startActivity(new Intent(SplashActivity.this,MainActivity.class));
        }

        finish();
    }
}
